//package com.service.impl;
//
//
//
//import com.entity.DictionaryEntry;
//import org.apache.commons.fileupload.FileItem;
//import org.apache.commons.fileupload.disk.DiskFileItemFactory;
//import org.apache.commons.fileupload.servlet.ServletFileUpload;
//import org.apache.poi.ss.usermodel.*;
//import org.apache.poi.xssf.usermodel.XSSFWorkbook;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.core.io.Resource;
//import org.springframework.core.io.ResourceLoader;
//import org.springframework.http.HttpEntity;
//import org.springframework.http.HttpHeaders;
//import org.springframework.http.HttpMethod;
//import org.springframework.http.ResponseEntity;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Service;
//import org.springframework.util.FileCopyUtils;
//import org.springframework.web.client.RestTemplate;
//import com.repository.DictionaryRepository;
//import com.service.DictionaryService;
//
//import javax.annotation.PostConstruct;
//import java.io.*;
//import java.net.URL;
//import java.sql.Timestamp;
//import java.time.LocalDateTime;
//import java.util.ArrayList;
//import java.util.List;
//
//@Service
//public class DictionaryServiceImpl implements DictionaryService {
//
//    private final DictionaryRepository dictionaryRepository;
//    private final ResourceLoader resourceLoader;
//    private final RestTemplate restTemplate;
//
//    @Value("${excel.online-url}")
//    private String onlineExcelUrl;
//
//    @Value("${excel.local-path}")
//    private String localExcelPath;
//
//    public DictionaryServiceImpl(DictionaryRepository dictionaryRepository, ResourceLoader resourceLoader, RestTemplate restTemplate) {
//        this.dictionaryRepository = dictionaryRepository;
//        this.resourceLoader = resourceLoader;
//        this.restTemplate = restTemplate;
//    }
//
//    @Scheduled(cron = "${scheduler.cron-expression}")
//    @Override
//    public void updateDictionaryFromExcel() throws IOException {
//        try {
//            // 优先从线上读取 Excel 文件
//            byte[] excelFile = downloadOnlineExcel();
//            if (excelFile == null) {
//                // 如果线上读取失败，尝试从本地读取
//                excelFile = readLocalExcel();
//            }
//            if (excelFile != null) {
//                // 解析 Excel 文件并更新字典表
//                List<DictionaryEntry> entries = parseExcel(excelFile);
//                updateDictionaryTable(entries);
//            } else {
//                System.out.println("无法读取 Excel 文件");
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
////第二种方案;
//    @Override
//    public byte[] downloadOnlineExcel() throws IOException {
//        try {
//            // 使用 RestTemplate 从线上 URL 下载 Excel 文件
//            HttpHeaders headers = new HttpHeaders();
//            HttpEntity<String> com.entity = new HttpEntity<>(headers);
//            ResponseEntity<byte[]> response = restTemplate.exchange(onlineExcelUrl, HttpMethod.GET, com.entity, byte[].class);
//
//            if (response.getStatusCode().is2xxSuccessful()) {
//                return response.getBody();
//            }
//        } catch (Exception e) {
//            System.out.println("无法从线上下载 Excel 文件: " + e.getMessage());
//        }
//        return null;
//    }
////第三种方案本地读取;
//    @Override
//    public byte[] readLocalExcel() throws IOException {
//        // 从本地文件系统读取 Excel 文件
//        Resource resource = resourceLoader.getResource(localExcelPath);
//        if (resource.exists()) {
//            try (InputStream inputStream = resource.getInputStream()) {
//                // 使用 FileCopyUtils 将 InputStream 转换为 byte 数组
//                return FileCopyUtils.copyToByteArray(inputStream);
//            }
//        } else {
//            throw new IOException("本地 Excel 文件不存在");
//        }
//    }
//    //解析线上文件进行存储到实体类里面;
//    @Override
//    public List<DictionaryEntry> parseExcel(byte[] file) throws IOException {
//        List<DictionaryEntry> entries = new ArrayList<>();
//        try (InputStream inputStream = new ByteArrayInputStream(file);
//             Workbook workbook = new XSSFWorkbook(inputStream)) {
//            Sheet sheet = workbook.getSheetAt(0);
//            int rowCount = sheet.getPhysicalNumberOfRows();
//            for (int i = 1; i < rowCount; i++) {
//                Row row = sheet.getRow(i);
//                if (row != null) {
//                    String dbName = getStringCellValue(row.getCell(0));
//                    String tableName = getStringCellValue(row.getCell(1));
//                    String fieldName = getStringCellValue(row.getCell(2));
//                    String indexName = getStringCellValue(row.getCell(3));
//                    LocalDateTime updateTime = LocalDateTime.now();
//
//                    DictionaryEntry entry = new DictionaryEntry();
//                    entry.setDbName(dbName);
//                    entry.setTableName(tableName);
//                    entry.setFieldName(fieldName);
//                    entry.setIndexName(indexName);
//                    entry.setUpdateTime(updateTime);
//
//                    entries.add(entry);
//                }
//            }
//        }
//        return entries;
//    }
//
//    private String getStringCellValue(Cell cell) {
//        if (cell == null) {
//            return null;
//        }
//        switch (cell.getCellType()) {
//            case STRING:
//                return cell.getStringCellValue();
//            case NUMERIC:
//                return String.valueOf(cell.getNumericCellValue());
//            case BOOLEAN:
//                return String.valueOf(cell.getBooleanCellValue());
//            case FORMULA:
//                return cell.getCellFormula();
//            default:
//                return null;
//        }
//    }
////进行定时任务的时候使用;
//    @Override
//    public void updateDictionaryTable(List<DictionaryEntry> entries) {
//        // 清除现有的字典表数据（可选）
//        dictionaryRepository.deleteAll();
//
//        // 批量插入新的字典条目
//        dictionaryRepository.saveAll(entries);
//
//        System.out.println("字典表已成功更新，共插入 " + entries.size() + " 条记录");
//    }
//}